package com.zfl.thread.day02;

class TicketSeller implements Runnable {

    int ticket = 50;
    Object lock = new Object();

    @Override
    public void run() {
        while (true) {
            synchronized (lock) {
                if (ticket > 0) {
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    ticket--;
                    System.out.println(Thread.currentThread().getName() + "卖出一次张票，还剩[" + ticket + "]张票");
                } else {
                    System.out.println(Thread.currentThread().getName() + "票已卖完");
                    break;
                }
            }
        }
    }
}

public class SynchronizedDemo01 {
    public static void main(String[] args) {
        TicketSeller ticketSeller = new TicketSeller();
        new Thread(ticketSeller).start();
        new Thread(ticketSeller).start();
        new Thread(ticketSeller).start();
        new Thread(ticketSeller).start();

    }
}
